package com.bclz.schedule;

import java.io.File;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Properties;
import java.util.TimerTask;

import org.apache.commons.io.FileUtils;

import com.bclz.config.Constants;
import com.bclz.config.LoadProperties;

/**
 * 
* @ClassName: CleanLogTask  
* @Description: 日志清理任务 
* @author xuchang  
* @date 2018年9月18日
 */
public class CleanLogTask extends TimerTask {

	@Override
	public void run() {
		// TODO Auto-generated method stub 
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH");
		ZoneId sysZone=ZoneId.systemDefault();
		
		try {
			
			long currentTime=Instant.now().toEpochMilli();
			Properties p=LoadProperties.loadHadoopConfig();
			int timeLimit=Integer.parseInt(p.getProperty(Constants.COLLECTOR_TIME_CLEAR));
			File[] backup=new File(p.getProperty(Constants.COLLECTOR_DIR_BACKUP)).listFiles();
			for (File file : backup) {
				if(file.isDirectory()) {
					long fileTime=LocalDateTime.parse(file.getName(), df)
					    .atZone(sysZone).toInstant().toEpochMilli();
					
					if((currentTime-fileTime)>timeLimit*60*1000) {
						FileUtils.deleteDirectory(file);
						System.out.println("Already Clear File Directory:"+file.getName());
					}
					
				}
				
				
			}
			System.out.println("All Already Clear!!");
			
			
		}catch(Exception e) {
			
			System.out.println("delete Failed");
		}
		
	}
	

}
